﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1
{
    public partial class GLForm : Form
    {
        //选中时的id
        int curr_id;
        public GLForm()
        {
            InitializeComponent();
        }

        private void GLForm_Load(object sender, EventArgs e)
        {
            if (!RuningData.FormDir.ContainsKey(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name))
            {
                RuningData.FormDir.Add(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name, this);
            }
            flushTable();
        }

        private void dataGridView1_Click(object sender, EventArgs e)
        {
            int index_x = dataGridView1.CurrentRow.Index;
            curr_id = Convert.ToInt32(dataGridView1.Rows[index_x].Cells[0].Value.ToString());
            textBox1.Text = dataGridView1.Rows[index_x].Cells[1].Value.ToString();
            textBox2.Text = dataGridView1.Rows[index_x].Cells[2].Value.ToString();
            textBox3.Text = dataGridView1.Rows[index_x].Cells[3].Value.ToString();
            textBox4.Text = dataGridView1.Rows[index_x].Cells[7].Value.ToString();
            textBox5.Text = dataGridView1.Rows[index_x].Cells[6].Value.ToString();
            textBox6.Text = dataGridView1.Rows[index_x].Cells[4].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = string.Format("UPDATE tb_Room SET RoomJC='{0}',RoomBJF={1},RoomWZ='{2}',RoomType='{3}',RoomBZ='{4}',RoomName='{5}' WHERE ID={6}"
                ,textBox2.Text,textBox3.Text,textBox6.Text,textBox5.Text,textBox4.Text,textBox1.Text,curr_id);
            int count =  DatabaseHelper.ExecuteUpdate(sql);
            if(count > 0)
            {
                MessageBox.Show("更新成功!");
                RoomModel room = new RoomModel();
                room.ID = curr_id;
                room.RoomJC = textBox2.Text.Trim();
                room.RoomBJF = Convert.ToInt32(textBox3.Text.Trim());
                room.RoomWZ = textBox6.Text.Trim();
                room.RoomType = textBox5.Text.Trim();
                room.RoomBZ = textBox4.Text.Trim();
                room.RoomName = textBox1.Text.Trim();
                RuningData.UpdateRoom(room);
            }
            else
            {
                MessageBox.Show("更新失败!");

            }
            flushTable();
        }

        private void flushTable()
        {
            DataSet ds = DatabaseHelper.GetDataSet("SELECT * FROM tb_Room");
            dataGridView1.DataSource = ds.Tables["temp"];
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string sql = string.Format("INSERT INTO tb_Room(RoomName,RoomJC,RoomBJF,RoomWZ,RoomType,RoomBZ) VALUES('{0}','{1}',{2},'{3}','{4}','{5}')"
                ,textBox1.Text,textBox2.Text,textBox3.Text,textBox6.Text,textBox5.Text,textBox4.Text);
            int count =  DatabaseHelper.ExecuteInsert(sql);
            if (count > 0)
            {
                MessageBox.Show("新增成功!");
                RoomModel room = new RoomModel();
                room.ID = curr_id;
                room.RoomJC = textBox2.Text.Trim();
                room.RoomBJF = Convert.ToInt32(textBox3.Text.Trim());
                room.RoomWZ = textBox6.Text.Trim();
                room.RoomType = textBox5.Text.Trim();
                room.RoomBZ = textBox4.Text.Trim();
                room.RoomName = textBox1.Text.Trim();
                RuningData.AddRoom(room);
            }
            else
            {
                MessageBox.Show("新增失败!");

            }
            this.flushTable();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            flushTable();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Hide();
            RuningData.FormDir["Form2"].Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            DataSet ds = DatabaseHelper.GetDataSet(string.Format("SELECT * FROM tb_Room WHERE RoomJC like '%{0}%'", textBox2.Text.Trim()));
            dataGridView1.DataSource = ds.Tables["temp"];
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
            textBox5.Text = string.Empty;
            textBox6.Text = string.Empty;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string sql = string.Format("DELETE FROM tb_Room WHERE ID={0}", curr_id.ToString());
            int count = DatabaseHelper.ExecuteDelete(sql);
            if(count > 0)
            {
                MessageBox.Show("删除成功!");
                RuningData.DeleteRoom(curr_id);
            }
            else
            {
                MessageBox.Show("删除失败!");
            }
            flushTable();
        }
    }
}
